package com.engine.kq.wfset.util;

import com.api.zszsj2.ZYT.util.SplitTimeInterval;
import com.engine.kq.biz.KQDutyRulesBiz;
import com.engine.kq.biz.chain.duration.*;
import com.engine.kq.enums.DurationTypeEnum;
import com.engine.kq.log.KQLog;
import com.engine.kq.wfset.bean.SplitBean;
import org.springframework.beans.BeanUtils;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;

import java.lang.reflect.InvocationTargetException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KQFlowDutyUtil {
    private KQLog kqLog = new KQLog();

    /**
     * 拆分加班数据 生成splitBeans
     *
     * @param sqlMap
     * @param splitBeans
     * @param datetimeFormatter
     * @param workflowId
     * @param requestId
     * @param rci
     * @param uuid
     * @return
     * @throws Exception
     */
    public Map<String, String> handleKQDutyAction(Map<String, String> sqlMap,  List<SplitBean> splitBeans
            , DateTimeFormatter datetimeFormatter, int workflowId, int requestId, ResourceComInfo rci) throws Exception {
        KQFlowUtil kqFlowUtil = new KQFlowUtil();

        RecordSet rs1 = new RecordSet();
        Map<String, String> result = new HashMap<>();
        if (!sqlMap.isEmpty()) {
            for (Map.Entry<String, String> me : sqlMap.entrySet()) {
                String key = me.getKey();
                String value = me.getValue();
                rs1.execute(value);
                while (rs1.next()) {
                    SplitBean splitBean = new SplitBean();
                    boolean isFillRight = kqFlowUtil.fillSplitBean(splitBean, rs1, "" + requestId, rci, "" + workflowId, DurationTypeEnum.DUTY, key, result, datetimeFormatter, "");
                    if (!isFillRight) {
                        return result;
                    }
                    List<SplitBean> tmp_splitBeans = new ArrayList<>();
                    doNonWorkSplitChain(splitBean, tmp_splitBeans);
                     splitBeans.addAll(tmp_splitBeans);
                }
            }
        }
        return result;
    }

    /**
     * 计算非工作时长拆分
     *
     * @param splitBean
     * @param splitBeans
     * @throws Exception
     */
    public void doNonWorkSplitChain(SplitBean splitBean, List<SplitBean> splitBeans)  {

        List<Map<String, Object>> _tmpDate = SplitTimeInterval.getDateDifference(splitBean.getFromDate() + " " + splitBean.getFromTime(), splitBean.getToDate() + " " + splitBean.getToTime());

        _tmpDate.stream().forEach(map -> {
                SplitBean _tmpBean = new SplitBean();
                BeanUtils.copyProperties(splitBean, _tmpBean);
                String startDate = map.get("startDate") + "";
                String endDate = map.get("endDate") + "";
                String duration = map.get("duration") + "";
                _tmpBean.setFromDate(startDate.split(" ")[0]);
                _tmpBean.setFromTime(startDate.split(" ")[1]);
                _tmpBean.setToDate(endDate.split(" ")[0]);
                _tmpBean.setToTime(endDate.split(" ")[1]);
                _tmpBean.setDuration(duration);
                _tmpBean.setD_Mins(Double.parseDouble(duration) * 60);
                _tmpBean.setBelongDate(splitBean.getFromDate());
                splitBeans.add(_tmpBean);

            }

        );
//        _tmpDate.forEach(map -> {
//            SplitBean _tmpBean = new SplitBean();
//            try {
//                BeanUtils.copyProperties(_tmpBean, splitBean);
//            } catch (IllegalAccessException e) {
//                e.printStackTrace();
//            } catch (InvocationTargetException e) {
//                e.printStackTrace();
//            }
//            String startDate = map.get("startDate") + "";
//            String endDate = map.get("endDate") + "";
//            String duration = map.get("duration") + "";
//            _tmpBean.setFromDate(startDate.split(" ")[0]);
//            _tmpBean.setFromTime(startDate.split(" ")[1]);
//            _tmpBean.setToDate(endDate.split(" ")[0]);
//            _tmpBean.setToTime(endDate.split(" ")[1]);
//            _tmpBean.setDuration(duration);
//            _tmpBean.setD_Mins(Double.parseDouble(duration) * 60);
//            _tmpBean.setBelongDate(splitBean.getFromDate());
//            splitBeans.add(_tmpBean);
//        });

    }


    /**
     * 加班流程单独需要赋值的数据
     *
     * @param prefix
     * @param rs1
     * @param splitBean
     */
    public static void bean4Duty(String prefix, RecordSet rs1, SplitBean splitBean) {
        String duty_type = Util.null2s(rs1.getString(prefix + "duty_type"), "-1");
        String minimumUnit = Util.null2s(Util.null2String(KQDutyRulesBiz.getMinimumUnit()), "-1");
        String computingMode = "1";
        splitBean.setDurationrule(minimumUnit);
        splitBean.setComputingMode(computingMode);
        splitBean.setDuty_type(duty_type);
    }




    public static List<LocalDate> splitByDay(LocalDate startDate, LocalDate endDate) {
        List<LocalDate> result = new ArrayList<>();

        LocalDate currentDate = startDate;

        while (!currentDate.isAfter(endDate)) {
            result.add(currentDate);
            currentDate = currentDate.plusDays(1);
        }

        return result;
    }


}
